﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_quan_ly_chuyen_muc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPostsCategories();
        }
    }
    protected void grvPostsCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        int id = Convert.ToInt32(grvPostsCategories.DataKeys[e.RowIndex].Value);
        Dal.CommandText = "SP_DeletePostsCategories";
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.ParameterCollection = new string[1] { "@ChuDeID" };
        Dal.ValueCollection = new string[1] { Convert.ToString(id) };
        Dal.ExecuteNonQuery();
        Dal.Close();
        Dal.Dispose();
        GetPostsCategories();
    }
    protected void grvPostsCategories_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvPostsCategories.EditIndex = e.NewEditIndex;
        GetPostsCategories();
        ((TextBox)grvPostsCategories.Rows[e.NewEditIndex].Cells[0].Controls[0]).Enabled = true;
    }
    protected void grvPostsCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        int id = Convert.ToInt32(grvPostsCategories.DataKeys[e.RowIndex].Value);
        string TenChuDe = ((TextBox)grvPostsCategories.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        Dal.CommandText = "SP_UpdatePostsCategories";
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.ParameterCollection = new string[2] { "@TenChuDe", "@ChuDeID" };
        Dal.ValueCollection = new string[2] { TenChuDe, Convert.ToString(id) };
        Dal.ExecuteNonQuery();
        grvPostsCategories.EditIndex = -1;
        Dal.Close();
        Dal.Dispose();
        GetPostsCategories();
    }
    protected void grvPostsCategories_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvPostsCategories.EditIndex = -1;
        GetPostsCategories();
    }
    protected void GetPostsCategories()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandText = "SP_GetAllPostscategories";
            Dal.CommandType = CommandType.StoredProcedure;
            DataTable db = Dal.GetDataTable();
            grvPostsCategories.DataSource = db;
            grvPostsCategories.DataBind();
        }
        catch { }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AdPostsCategories";
            Dal.ParameterCollection = new string[1] { "@TenChuDe"};
            Dal.ValueCollection = new string[1] { txtPostsCategories.Text};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            lblInfo.Text = "Thêm mới thành công !";
            GetPostsCategories();
            txtPostsCategories.Text = "";
        }
        catch(Exception ex) {
            lblInfo.Text = ex.Message;
        }
    }
}
